Imports System.Collections.Generic
Imports System.Text

Namespace FTP
    ''' <summary>
    ''' The type of item.
    ''' </summary>
    Public Enum FtpItemType
        ''' <summary>
        ''' No item type specified.
        ''' </summary>
        None
        ''' <summary>
        ''' Directory item.
        ''' </summary>
        Directory
        ''' <summary>
        ''' File item.
        ''' </summary>
        File
        ''' <summary>
        ''' Symbolic link item.
        ''' </summary>
        SymbolicLink
        ''' <summary>
        ''' Block special file item.
        ''' </summary>
        BlockSpecialFile
        ''' <summary>
        ''' Character special file item.
        ''' </summary>
        CharacterSpecialFile
        ''' <summary>
        ''' Name socket item.
        ''' </summary>
        NamedSocket
        ''' <summary>
        ''' Domain socket item.
        ''' </summary>
        DomainSocket
        ''' <summary>
        ''' Pathname of the directory whose contents are listed. 
        ''' </summary>
        CurrentDirectory
        ''' <summary>
        ''' Parent directory. 
        ''' </summary>
        ParentDirectory
        ''' <summary>
        ''' Other item type.
        ''' </summary>
        Other
    End Enum

    ''' <summary>
    ''' Base class for the FTP item.
    ''' </summary>
    Public Class FtpItem
        Private _rawText As String
        Private _parentPath As String
        Private _itemType As FtpItemType
        Private _name As String
        Private _size As Long
        Private _modified As System.Nullable(Of DateTime)
        Private _attributes As String
        Private _mode As System.Nullable(Of Int32)
        Private _symbolicLink As String

        ''' <summary>
        ''' Default constructor.
        ''' </summary>
        Public Sub New()
        End Sub

        ''' <summary>
        ''' Constructor to create a new ftp item.
        ''' </summary>
        ''' <param name="name">Name of the item.</param>
        ''' <param name="modified">Modified date and/or time of the item.</param>
        ''' <param name="size">Number of bytes or size of the item.</param>
        ''' <param name="itemType">Type of the item.</param>
        ''' <param name="attributes">UNIX style permission attribute text for item.</param>
        ''' <param name="mode">UNIX style mode permission integer value.</param>
        ''' <param name="symbolicLink">UNIX style symbolic linked file name .</param>
        ''' <param name="rawText">The raw text of the item.</param>
        Public Sub New(name As String, modified As System.Nullable(Of DateTime), size As Long, itemType As FtpItemType, attributes As String, mode As System.Nullable(Of Int32), _
            symbolicLink As String, rawText As String)
            _name = name
            _modified = modified
            _size = size
            _itemType = itemType
            _attributes = attributes
            _mode = mode
            _symbolicLink = symbolicLink
            _rawText = rawText
        End Sub


        ''' <summary>
        ''' Gets the raw text line for the item.
        ''' </summary>
        Public ReadOnly Property RawText() As String
            Get
                Return _rawText
            End Get
        End Property

        ''' <summary>
        ''' Gets the parent path for the item.
        ''' </summary>
        Public ReadOnly Property ParentPath() As String
            Get
                Return _parentPath
            End Get
        End Property

        ''' <summary>
        ''' Internal method to set the parent path value.
        ''' </summary>
        ''' <param name="path">Parent path.</param>
        Friend Sub SetParentPath(path As String)
            _parentPath = path
        End Sub

        ''' <summary>
        ''' Gets the name of the item.
        ''' </summary>
        Public ReadOnly Property Name() As String
            Get
                Return _name
            End Get
        End Property

        ''' <summary>
        ''' Gets the size of the file or directory.
        ''' </summary>
        Public ReadOnly Property Size() As Long
            Get
                Return _size
            End Get
        End Property

        ''' <summary>
        ''' Gets the last modification date and time.  Adjusted to from UTC (GMT) to local time.
        ''' </summary>
        Public ReadOnly Property Modified() As System.Nullable(Of DateTime)
            Get
                Return _modified
            End Get
        End Property

        ''' <summary>
        ''' Gets the item type.
        ''' </summary>
        Public ReadOnly Property ItemType() As FtpItemType
            Get
                Return _itemType
            End Get
        End Property

        ''' <summary>
        ''' UNIX style permissions text for the item.  
        ''' </summary>
        Public ReadOnly Property Attributes() As String
            Get
                Return _attributes
            End Get
        End Property

        ''' <summary>
        ''' UNIX style mode permission integer value for the item.  
        ''' </summary>
        Public ReadOnly Property Mode() As System.Nullable(Of Int32)
            Get
                Return _mode
            End Get
        End Property

        ''' <summary>
        ''' The symbolic link name if the item is of itemType symbolic link.
        ''' </summary>
        Public ReadOnly Property SymbolicLink() As String
            Get
                Return _symbolicLink
            End Get
        End Property

        ''' <summary>
        ''' Item full path.
        ''' </summary>
        Public ReadOnly Property FullPath() As String
            Get
                Return If(_parentPath = "/" OrElse _parentPath = "//", [String].Format("{0}{1}", _parentPath, _name), [String].Format("{0}/{1}", _parentPath, _name))
            End Get
        End Property

        ''' <summary>
        ''' Clone the current object.
        ''' </summary>
        ''' <return></return>
        Public Overridable Function Clone() As FtpItem
            Dim c As New FtpItem(_name, _modified, _size, _itemType, _attributes, _mode, _
                _symbolicLink, _rawText)
            Return c
        End Function

        ''' <summary>
        ''' Gets the item base properties as string.
        ''' </summary>
        ''' <return>String object.</return>
        Public Overrides Function ToString() As String
            Dim b As New StringBuilder()
            b.AppendFormat("ParentPath={0}" & vbCr & vbLf, _parentPath)
            b.AppendFormat("Name={0}" & vbCr & vbLf, _name)
            b.AppendFormat("Size={0}" & vbCr & vbLf, _size)
            If _modified IsNot Nothing Then
                b.AppendFormat("Modified={0}" & vbCr & vbLf, _modified)
            Else
                b.AppendFormat("Modified=null" & vbCr & vbLf)
            End If
            b.AppendFormat("ItemType={0}" & vbCr & vbLf, _itemType)
            b.AppendFormat("Attributes{0}" & vbCr & vbLf, _attributes)
            If _mode IsNot Nothing Then
                b.AppendFormat("Mode={0}" & vbCr & vbLf, _mode)
            Else
                b.AppendFormat("Mode=null" & vbCr & vbLf)
            End If
            b.AppendFormat("SymbolicLink={0}" & vbCr & vbLf, _symbolicLink)

            Return b.ToString()
        End Function
    End Class
End Namespace
